from libs.base.decorators import check_login, cache_request, check_form
from libs.base.web import BaseHandler
from . import forms
from . import models


class HomepageHandler(BaseHandler):

    @check_login()
    async def get(self):
        """
        @api {GET} /api/v1.0/demo/common/homepage 首页数据
        @apiVersion 1.0.0
        @apiName GetHomepage
        @apiGroup DemoCommon
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiSuccess (Response-data) total_data  统计总值
        @apiSuccess (Response-data) total_data.a  总销售额
        @apiSuccess (Response-data) total_data.b  订单
        @apiSuccess (Response-data) total_data.c  访客
        @apiSuccess (Response-data) total_data.d  活跃用户
        @apiSuccess (Response-data) column_graph  柱状图数据

        @apiSuccessExample {json} Success-Response:
        {
            "status": 0,
            "message": "请求成功",
            "data": {
                "total_data": {
                    "a": 88559,
                    "b": 2898,
                    "c": 224,
                    "d": 91
                },
                "column_graph": [
                    ["1月", 699],
                    ["2月", 699],
                    ["3月", 699]
                ]
            }
        }
        """
        model = models.HomepageModel(self)
        res = await model.stat_data()
        self.finish_auto(**res)


class RegionHandler(BaseHandler):

    @check_login()
    @cache_request()
    async def get(self):
        """
        @api {GET} /api/v1.0/demo/common/region 区域数据
        @apiVersion 1.0.0
        @apiName GetRegion
        @apiGroup DemoCommon
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiSuccess (Response-data) region_code  区域编码(国家/省/市)
        @apiSuccess (Response-data) father_code  父级编码
        @apiSuccess (Response-data) region_class  行政区类别(1.国家, 2.省, 3.市)
        @apiSuccess (Response-data) region_name  区域名称
        @apiSuccess (Response-data) region_name_eng  区域英文名称

        @apiSuccessExample {json} Success-Response:
        {
            "status": 0,
            "message": "请求成功",
            "data": [
                {
                    "region_code": "101",
                    "father_code": "1",
                    "region_class": 1,
                    "region_name": "中国",
                    "region_name_eng": "China"
                },
                {
                    "region_code": "101101",
                    "father_code": "101",
                    "region_class": 2,
                    "region_name": "北京",
                    "region_name_eng": "Beijing"
                }
            ]
        }
        """
        model = models.GlobalDataModel(self)
        res = await model.get_region()
        self.finish_auto(**res)


class AutoCompleteUserHandler(BaseHandler):

    @check_login()
    @cache_request(30)
    @check_form(forms.AutoCompleteUserForm)
    async def get(self):
        """
        @api {GET} /api/v1.0/demo/common/auto_complete/user 用户自动补全
        @apiVersion 1.0.0
        @apiName GetUserAutoComplete
        @apiGroup DemoCommon
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiSuccess (Response-data) key  用户姓名关键字

        @apiSuccessExample {json} Success-Response:
        {
            "status": 0,
            "message": "请求成功",
            "data": [
                {
                    "region_code": "101",
                    "father_code": "1",
                    "region_class": 1,
                    "region_name": "中国",
                    "region_name_eng": "China"
                },
                {
                    "region_code": "101101",
                    "father_code": "101",
                    "region_class": 2,
                    "region_name": "北京",
                    "region_name_eng": "Beijing"
                }
            ]
        }
        """
        model = models.AutoCompleteModel(self)
        res = await model.get_user(self.form_data)
        self.finish_auto(**res)
